﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Award
{
    public partial class AwardMain : Form
    {
        public delegate void SetBlinkingNamesDelegate(string[] Persons);
        public delegate void CommonInputStringDelegate(string Value);
        public AwardMain()
        {
            this.dShowMessageBox = new CommonInputStringDelegate(this.ShowMessageBox);
            this.dSetStatistics = new CommonInputStringDelegate(this.SetStatistics);
            this.dUpdateBlinking = new SetBlinkingNamesDelegate(this.SetBlinkingNames);
            this.dSetSelectButtonText = new CommonInputStringDelegate(this.SetSelectButtonText);

            InitializeComponent();
            
            this.selector = new AwardSelection(this);
            
            this.SuspendLayout();
            Bitmap bmpBG = new Bitmap(Application.StartupPath + "\\bg.jpg");
            this.tlpBottom.BackgroundImage = bmpBG;
            Bitmap bmpSB = new Bitmap(Application.StartupPath + "\\selectbtn.jpg");
            this.btnSelect.BackgroundImage = bmpSB;
            Bitmap bmpRB = new Bitmap(Application.StartupPath + "\\resultbtn.jpg");
            this.btnShowRecord.BackgroundImage = bmpRB;
            
            this.btnSelect.ForeColor = Properties.Settings.Default.抽奖按钮文字颜色;
            this.tbBlinking.BackColor = Properties.Settings.Default.候选人员列表区背景颜色;
            this.tbBlinking.Font = Properties.Settings.Default.候选人员列表字体;
            this.lblTitle.Font = Properties.Settings.Default.标题文字字体;
            this.lblTitle.Text = Properties.Settings.Default.标题文字内容;
            this.lblTitle.ForeColor = Properties.Settings.Default.标题文字颜色;
            this.lblStatistics.Font = Properties.Settings.Default.统计信息区字体;
            this.lblStatistics.ForeColor = Properties.Settings.Default.统计信息区文字颜色;
            this.ResumeLayout();
        }

        private void ShowMessageBox(string Message)
        {
            MessageBox.Show(this, Message);
        }

        private AwardSelection selector;
        private SetBlinkingNamesDelegate dUpdateBlinking;
        private CommonInputStringDelegate dSetStatistics;
        private CommonInputStringDelegate dSetSelectButtonText;
        public CommonInputStringDelegate dShowMessageBox;
        private bool GoOnSelect = false;

        private void AwardMain_Load(object sender, EventArgs e)
        {
            this.btnSelect.Focus();
            this.btnSelect.Select();
            this.selector.LoadPersonList();
            this.selector.InitiallizeAwardLevel();
            this.dSetStatistics(this.selector.GetCurrentStatistics());
        }
        private void SetBlinkingNames(string[] Persons)
        {
            int iFontSize = (int)(Properties.Settings.Default.候选人员列表区字体缩放比例
                * Math.Sqrt(this.tbBlinking.Height * this.tbBlinking.Width)
                / Math.Sqrt(Persons.Length));
            if (iFontSize < 2 || iFontSize > 500)
            {
                return;
            }
            if ((int)this.tbBlinking.Font.SizeInPoints != iFontSize)
            {
                this.tbBlinking.Font = new Font(this.tbBlinking.Font.FontFamily,
                    iFontSize, this.tbBlinking.Font.Style, GraphicsUnit.Point);
            }
            this.tbBlinking.Text = Persons.Aggregate((s1, s2) => s1 + " " + s2);
            this.tbBlinking.Invalidate();
        }
        private void SetStatistics(string Value)
        {
            int iFontSize = (int)(Properties.Settings.Default.统计信息区字体缩放比例
                * this.lblStatistics.Height / Value.Count(c => c == '\n'));
            if (iFontSize < 2 || iFontSize > 500)
            {
                return;
            }
            if ((int)this.lblStatistics.Font.SizeInPoints != iFontSize)
            {
                this.lblStatistics.Font = new Font(this.lblStatistics.Font.FontFamily,
                    iFontSize, this.lblStatistics.Font.Style, GraphicsUnit.Point);
            }
            this.lblStatistics.Text = Value;
            this.lblStatistics.Invalidate();
        }
        private void btnSelect_Click(object sender, EventArgs e)
        {
            lock (this)
            {
                if (this.GoOnSelect)
                {
                    this.btnSelect.Text = "开始抽奖";
                    this.GoOnSelect = false;
                }
                else
                {
                    this.GoOnSelect = true;
                    System.Threading.Thread th = new System.Threading.Thread(
                        new System.Threading.ParameterizedThreadStart(this.StartSelect));
                    th.Start();
                }
            }
        }
        private void SetSelectButtonText(string Value)
        {
            this.btnSelect.Text = Value;
        }
        private void StartSelect(object Nothing)
        {
            bool bGoOn = false;
            lock (this)
            {
                bGoOn = this.GoOnSelect;
            }
            this.btnSelect.Invoke(this.dSetSelectButtonText, new object[] { "停止抽奖" });
            while (bGoOn)
            {
                lock (this)
                {
                    bGoOn = this.GoOnSelect;
                }
                string[] saBlinkingPersons = this.selector.RandomSelectPerson();
                if (saBlinkingPersons == null)
                {
                    lock (this)
                    {
                        this.Invoke(this.dShowMessageBox, new object[]{"抽奖数据状态错误，请重新初始化。"});
                    }
                    this.GoOnSelect = false;
                    break;
                }
                this.tbBlinking.Invoke(this.dUpdateBlinking, new object[] { saBlinkingPersons });
                System.Threading.Thread.Sleep(Properties.Settings.Default.候选人员列表轮询间隔);
            }
            this.selector.ConfirmSelectedPersons();
            this.lblStatistics.Invoke(this.dSetStatistics, new object[] { this.selector.GetCurrentStatistics() });
            System.IO.StreamWriter sw = new System.IO.StreamWriter(
                Application.StartupPath + "\\Result.txt", true, Encoding.UTF8);
            sw.WriteLine(DateTime.Now.ToString());
            sw.Write(this.selector.GetCurrentSelectedPersons().Select(
                t => t.Item1 + ":" + t.Item2).Aggregate((s1, s2) => s1 + "\r\n" + s2));
            sw.Write("\r\n\r\n");
            sw.Flush();
            sw.Close();
            sw.Dispose();
            this.btnSelect.Invoke(this.dSetSelectButtonText, new object[] { "开始抽奖" });
        }

        private void btnSelect_Resize(object sender, EventArgs e)
        {
            this.btnSelect.Font = new Font(
                Properties.Settings.Default.抽奖按钮文字字体.FontFamily,
                (float)(this.btnSelect.Height * 0.55),
                Properties.Settings.Default.抽奖按钮文字字体.Style,
                GraphicsUnit.Point);
        }

        private void btnShowRecord_Resize(object sender, EventArgs e)
        {
            this.btnShowRecord.Font = new Font(
                Properties.Settings.Default.结果按钮文字字体.FontFamily,
                (float)(this.btnShowRecord.Width * 0.5),
                Properties.Settings.Default.结果按钮文字字体.Style,
                GraphicsUnit.Point);
            this.btnShowRecord.ForeColor = Properties.Settings.Default.结果按钮就文字颜色;
        }

        private void btnShowRecord_Click(object sender, EventArgs e)
        {
            Tuple<string, string>[] taResult = this.selector.GetCurrentSelectedPersons();
            if (taResult == null)
            {
                MessageBox.Show("奖项设置尚未初始化，请初始化后查看当前结果");
            }
            else
            {
                MessageBox.Show(taResult.Select(t => t.Item1 + ":" + t.Item2).Aggregate((s1, s2) => s1 + "\r\n" + s2));
            }
        }

        private void 清除当前奖项结果ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.selector.ClearCurrentAwardLevel();
            this.SetStatistics(this.selector.GetCurrentStatistics());
            this.tbBlinking.Text = "";
            MessageBox.Show("当前奖项获奖结果已清空，可以重新开始抽奖了。");
        }

        private void 重新载入ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.selector.LoadPersonList();
            this.selector.InitiallizeAwardLevel();
            this.SetStatistics(this.selector.GetCurrentStatistics());
            this.tbBlinking.Text = "";
            MessageBox.Show("全部奖项设置已重新加载，现在可以重新开始抽奖了。");
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
